package cn.bonoon.controllers.shortcutMenu;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bonoon.core.plugins.ShortcutMenuService;
import cn.bonoon.entities.plugins.ShortcutMenuEntity;
import cn.bonoon.kernel.util.StringHelper;
import cn.bonoon.kernel.web.controllers.AbstractController;
import cn.bonoon.kernel.web.models.JsonResult;

/**
 * 快捷菜单
 * 
 * @author ocean~
 */
@Controller
@RequestMapping("s/user/shortcutMenu")
public class ShortcutMenuController extends AbstractController {
	@Autowired
	private ShortcutMenuService shortcutMenuService;

	@ResponseBody
	@RequestMapping(value = "save", method = { RequestMethod.GET, RequestMethod.POST })
	public JsonResult save(HttpServletRequest request, String mid, String rename, Integer type) {
		try {
			shortcutMenuService.save(getUser(), mid, rename, type);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.error("数据异常，操作失败！");
		}
		return JsonResult.result();
	}

	@ResponseBody
	@RequestMapping(value = "update/name", method = { RequestMethod.GET, RequestMethod.POST })
	public JsonResult update_name(HttpServletRequest request, Long id, String name) {
		try {
			if (StringHelper.isNotEmpty(name)) {
//				OperateEvent event = new OperateEvent(request, null, getUser());
//				event.setScope(VisibleScope.EXCLUSIVE);
				ShortcutMenuEntity entity = shortcutMenuService.get(id);
				entity.setName(name);
				shortcutMenuService.update(/*event, */entity);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.error("数据异常，操作失败！");
		}
		return JsonResult.result();
	}

	@ResponseBody
	@RequestMapping(value = "update/ordinal", method = { RequestMethod.GET, RequestMethod.POST })
	public JsonResult update_ordinal(HttpServletRequest request, String ids) {
		try {
			shortcutMenuService.sort(getUser(), StringHelper.toLongArray(ids));
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.error("数据异常，操作失败！");
		}
		return JsonResult.result();
	}

	@ResponseBody
	@RequestMapping(value = "update/module", method = { RequestMethod.GET, RequestMethod.POST })
	public JsonResult update_module(HttpServletRequest request, Long id, Integer type) {
		try {
			shortcutMenuService.update_module(getUser(), id, type);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.error("数据异常，操作失败！");
		}
		return JsonResult.result();
	}

	@ResponseBody
	@RequestMapping(value = "detele", method = { RequestMethod.GET, RequestMethod.POST })
	public JsonResult detele(HttpServletRequest request, Long id) {
		try {
//			OperateEvent event = new OperateEvent(request, null, getUser());
//			event.setScope(VisibleScope.EXCLUSIVE);
			ShortcutMenuEntity entity = shortcutMenuService.get(id);
			shortcutMenuService.delete(/*event, */entity);
		} catch (Exception e) {
			e.printStackTrace();
			return JsonResult.error("数据异常，操作失败！");
		}
		return JsonResult.result();
	}
}
